package com.hb.trans.mvp.presenter

import com.basiclib.base.BasePresenter
import com.basiclib.lifecycle.RxLifecycleUtils
import com.basiclib.network.RetryWithDelay
import com.basiclib.network.RxErrorHandler
import com.hb.trans.bean.Base
import com.hb.trans.bean.Coach
import com.hb.trans.global.ErrorHandleSubscriber
import com.hb.trans.mvp.contract.CoachInfoContract
import com.hb.trans.mvp.model.CoachInfoModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

class CoachInfoPresenter @Inject constructor(model: CoachInfoModel, var errorHandler: RxErrorHandler) :
    BasePresenter<CoachInfoContract.View, CoachInfoContract.Model>(model) {
    fun getCoachInfo(coanum: String) {
        mModel.getCoachInfo(coanum)
            .subscribeOn(Schedulers.io())
            .retryWhen(RetryWithDelay())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView!!))
            .subscribe(object : ErrorHandleSubscriber<Base<Coach>>(errorHandler) {
                override fun next(r: Base<Coach>) {
                    if (r.code == 200) {
                        mRootView?.setupCoachInfo(r.data)
                    } else {
                        mRootView?.showShortToast(r.info ?: "")
                    }
                }
            })
    }

}